/**
 * @file snippet/dp/knapsack/0-1knapsack/main
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 5/18/2022 20:47:07
 **/

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int N = INF;
const int T = 1000 + 10, M = 100;
ll dp[T];
int main() {
  // High rating and good luck!
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t, m;
  cin >> t >> m;
  for (int i = 1; i <= m; ++i) {
    int v, w;
    cin >> v >> w;
    for (int j = t; j >= 0; --j) {
      if (j - v >= 0) {
        dp[j] = max(dp[j], dp[j - v] + w);
      }
    }
  }
  cout << dp[t] << endl;

  return 0;
}
